<script setup>
import { ref, toRefs } from 'vue'
const props = defineProps({
  title: {
    required: true,
    type: String
  },
  visible: {
    required: true,
    type: Boolean
  },
  formData: {
    required: false
  }
})
const emits = defineEmits(['confirm', 'cancel'])
const { visible, title, formData } = toRefs(props)
// 表单验证
const formRef = ref()
const rules = {
  name: [
    { required: true, message: '请输入类型名称', trigger: 'blur' },
    { min: 2, max: 255, message: '长度是 2-255 位字符', trigger: 'blur' }
  ]
}

// 关闭窗口，触发父组件事件
const onCancel = (formRef) => {
  if (!formRef) return
  formRef.resetFields()
  emits('cancel')
}
//提交数据，触发父组件事件
const onConfirm = async (formRef) => {
  if (!formRef) return
  await formRef.validate((valid) => {
    if (valid) {
      emits('confirm', formData.value)
    }
  })
}
</script>

<template>
  <el-dialog
    v-model="visible"
    :title="title"
    width="40%"
    @close="onCancel(formRef)"
  >
    <el-form
      label-width="80"
      label-position="left"
      :model="formData"
      ref="formRef"
      :rules="rules"
      size="large"
      autocomplete="off"
      destroy-on-close
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      status-icon
    >
      <el-form-item label="类型名称" prop="name">
        <el-input
          v-model="formData.name"
          placeholder="请输入类型名称"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="onCancel(formRef)">取消</el-button>
        <el-button type="primary" @click="onConfirm(formRef)"> 确定 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss">
:deep .el-dialog {
  width: 50rem;
}
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
:deep .avatar-uploader .el-upload {
  border: 3px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
